package xutil

import (
	"crypto/rsa"
	"fmt"
	"net/url"
	"os"

	"gitee.com/wosylf/ltool"
	"gitee.com/wosylf/ltool/encrypt"
	"gitee.com/wosylf/ltool/pkg/xhttp"
)

const (
	V请求域名_沙箱  = "http://sit-shouwei.shouxin168.com/sandbox" //测试
	V请求域名_正式  = "https://shouwei.shouxin168.com/api"        //正式
	V天狼星报告星耀版 = "/lightning/product/query"
	V统一接口地址   = "/lightning/product/query"
	V合同上传地址   = "/contract/file/upload"
)

type S布尔 struct {
	PrivateKey *rsa.PrivateKey
	Iid        string //id
	AES_KEY    string //加密解密的
	Notifiy    string //密钥
	Mode       string //密钥
	V沙箱        bool
	Desc       encrypt.S加解密接口
}

var (
	V布尔链接 *S布尔
)

func F新建布尔链接(InstitutionId, AES_KEY, notifiy, mode string) (client *S布尔, err error) {
	client = &S布尔{
		Iid:     InstitutionId,
		AES_KEY: AES_KEY,
		Notifiy: notifiy,
		Mode:    mode,
		Desc:    encrypt.F新建Ase配置(AES_KEY, "ECB", "base64"),
	}
	return client, nil
}
func (a *S布尔) F查询星耀报告(id, name, phone, 完整路径 string) (by []byte, err error) {

	bm := make(ltool.BodyMap)
	bm.Set("ident_number", id).
		Set("name", name).Set("phone", phone).
		Set("mode", a.Mode).
		Set("licenseUrl", 完整路径).
		Set("licenseType", 1).
		Set("service", "risk_report_service")
	by, err = a.F发送请求(bm, V天狼星报告星耀版)
	return
}

func (a *S布尔) F企业认证(id, name, phone string) (by []byte, err error) {
	bm := make(ltool.BodyMap)
	bm.Set("orgCode", id).
		Set("name", name).Set("notifyUrl", a.Notifiy).
		Set("mode", "mode_company_identity_auth").
		Set("phone", phone).
		Set("service", "online_signature_service")
	by, err = a.F发送请求(bm, V统一接口地址)
	return
}
func (a *S布尔) F个人认证(id, name, phone string) (by []byte, err error) {
	bm := make(ltool.BodyMap)
	bm.Set("orgCode", id).
		Set("name", name).Set("notifyUrl", a.Notifiy).
		Set("mode", "mode_company_identity_auth").
		Set("phone", phone).
		Set("service", "online_signature_service")
	by, err = a.F发送请求(bm, V统一接口地址)
	return
}

func F上传合同(filepath string) {
	fileContent, err := os.ReadFile(filepath)
	if err != nil {
		return
	}
	bm := make(ltool.BodyMap)
	bm.SetFormFile("file", &ltool.File{
		Name:    "file",
		Content: fileContent,
	})
	fmt.Println("进入合同上传")
	httpClient := xhttp.NewClient()
	_, bs, err := httpClient.Type(xhttp.TypeMultipartFormData).Post(V合同上传地址).
		SendMultipartBodyMap(bm).EndBytes(ctx)
	if err != nil {
		fmt.Println("错误信息", err.Error())
	}
	fmt.Println("请求的结果是", string(bs))
}

func (a *S布尔) F发送请求(bm ltool.BodyMap, url string) (bs []byte, err error) {
	bm1 := make(ltool.BodyMap)
	bm1.Set("institution_id", a.Iid)

	bm1.Set("biz_data", a.F获取biz数据(bm))
	//sign = a.F获取签名(bm)
	//bm1.Set("sign", a.F获取签名(bm))
	完整路径 := V请求域名_正式 + url
	if a.V沙箱 {
		完整路径 = V请求域名_沙箱 + url
	}
	httpClient := xhttp.NewClient()
	httpClient.Header.Add("keepAlive", "false")
	_, bs, err = httpClient.Type(xhttp.TypeFormData).
		Post(完整路径).
		SendString(bm1.EncodeAliPaySignParams()).EndBytes(ctx)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	return
}

func (a *S布尔) F获取biz数据(bm ltool.BodyMap) (biz string) {
	jm, _ := a.Desc.F加密信息(bm.JsonBody())
	biz = UrlEncode(jm)
	return
}

func UrlEncode(str string) string {
	return url.QueryEscape(str)
}
